package whf.framework.i18n;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import whf.framework.config.ApplicationConfig;
import whf.framework.util.ThreadContext;
import whf.framework.util.Utils;

/**
 * 
 * @author wanghaifeng
 * @create Apr 17, 2007 11:14:20 PM
 * 
 */
public final class ApplicationResource {
	private static ApplicationResource instance = new ApplicationResource();
	
	/**
	 * Bundle名称
	 * @property String:applicationResource
	 */
	private String applicationResource = ApplicationConfig.getInstance().getI18n();
	
	/**
	 * 资源缓存
	 * @property Map<Locale,ResourceBundle>:resources
	 */
	private Map<Locale, ResourceBundle> resources = Utils.newHashMap();
	
	private ApplicationResource(){
		
	}
	
	public static ApplicationResource getInstance(){
		return instance;
	}
	
	/**
	 * @param locale	本地化
	 * @param key		键值
	 * @return
	 */
	public String getValue(final Locale locale, final String key){
		try{
			ResourceBundle bundle = null;
			if(locale != null)
				bundle = this.getResourceBundle(locale);
			else 
				bundle = this.getResourceBundle(Locale.getDefault());
			if(bundle.containsKey(key)){
				return bundle.getString(key);
			} else {
				return null;
			}
		}catch(Exception e){
			return null;
		}
	}
	
	/**
	 * @param locale 	本地化
	 * @param key		键值
	 * @param params	格式化参数
	 * @return
	 */
	public String getValue(final Locale locale, final String key, Object[] params){
		if(key == null) return null;
		ResourceBundle bundle = null;
		if(locale != null)
			bundle = this.getResourceBundle(locale);
		else 
			bundle = this.getResourceBundle(Locale.getDefault());
		if(bundle.containsKey(key)){
			return MessageFormat.format(bundle.getString(key), params);
		} else {
			return null;
		}
	}
	
	/**
	 * @param key	键值
	 * @return
	 */
	public String getValue(final String key){
		Locale locale = null;
		if(ThreadContext.getUserContext() != null) 
			locale = ThreadContext.getUserContext().getLocale();
		return getValue(locale, key);
	}
	
	private ResourceBundle getResourceBundle(final Locale locale){
		ResourceBundle res = resources.get(locale);
		if(res == null){
			res = ResourceBundle.getBundle(applicationResource, locale);
			resources.put(locale, res);
		}
		return res;
	}
	
	public static String get(final String key){
		return ApplicationResource.getInstance().getValue(key);
	}
	
	public static String get(final Locale locale, final String key){
		return ApplicationResource.getInstance().getValue(locale, key);
	}
	
	public static String get(final Locale locale, final String key, Object[] params){
		return ApplicationResource.getInstance().getValue(locale, key, params);
	}
	
	public static String get(final String key, final Object[] parameters){
		String value = ApplicationResource.getInstance().getValue(key);
		return MessageFormat.format(value, parameters);
	}
	
	public static String get(final String key, final Object parameters){
		String value = ApplicationResource.getInstance().getValue(key);
		return MessageFormat.format(value, parameters);
	}
	
	public static void main(String[] args) throws Exception{
		System.out.println(get("application.name"));
		System.out.println(get(Locale.ENGLISH, "application.name"));
	}
}
